package com.atguigu.flink.chapter08;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.time.Duration;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/10/19 9:12
 */
public class Test {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        env
            .socketTextStream("hadoop162", 9999)// 并行度用于是1
            .keyBy(line -> "abc")
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(data[0], Long.parseLong(data[1]) * 1000, Integer.valueOf(data[2]));
            })
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<WaterSensor>forMonotonousTimestamps()
                    .withTimestampAssigner((ws, ts) -> ws.getTs())
                    .withIdleness(Duration.ofSeconds(5))
            
            )
            .keyBy(WaterSensor::getId)
            .sum("vc")
            .print();
        
        env.execute();
    }
}
